<%--
  Created by IntelliJ IDEA.
  User: 王梦雨
  Date: 2024/9/25
  Time: 9:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*" %>
<% request.setCharacterEncoding("UTF-8");%>
<% String id = request.getParameter("stuid");%>
<% String stuName = request.getParameter("stuname");%>
<% String stuBirthday = request.getParameter("stubirthday");%>
<% String stuClass = request.getParameter("stuclass");%>
<html>
<head>
    <title>条件查询</title>
  <%
    String dbName = "school";//数据库
    String userName = "root";//账户
    String userPassword = "wmy123";//密码
    String url = "jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPassword;
    Class.forName("com.mysql.cj.jdbc.Driver");//5.7以下不加.cj
    Connection conn = DriverManager.getConnection(url);//连接数据库
    Statement statement = conn.createStatement();//创建操作对象
    String sql = "insert into student values('"+id+"','"+stuName+"','"+stuBirthday+"','"+stuClass+"')";
    int result = statement.executeUpdate(sql);//运行sql命令查询数据 结果放到rs结果集中
    if(result > 0){
      out.print("<script>alert('添加成功')</script>");
    }else {
      out.print("<script>alert('添加失败')</script>");
    }
    sql = "select * from student";//查询学生表所有字段
    ResultSet rs = null;
    rs = statement.executeQuery(sql);//运行sql命令查询数据 结果放到rs结果集中
  %>
  <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
<table class="layui-table" lay-even>
  <colgroup>学生信息表</colgroup>
  <thead>
  <tr>
    <th>学号</th>
    <th>姓名</th>
    <th>所在班级</th>
  </tr>
  </thead>
  <tbody>
<% while (rs.next()) { %>
  <tr>
    <td><%=rs.getString(1)%></td>
    <td><%=rs.getString(2)%></td>
    <td><%=rs.getString(3)%></td>
    <td><%=rs.getString(4)%></td>
  </tr>
<% } %>
  </tbody>
  <%
  rs.close();
  statement.close();
  conn.close();
  %>
</table>
</body>
</html>
